﻿using System;
using System.Activities;
using System.ComponentModel;

namespace Vision.Core.ActivityComponents.Common
{
    /// <summary>
    /// 
    /// </summary>
    [Designer(typeof(WhileDesigner))]
    public class While : NativeActivity
    {
        [Browsable(false)]
        public Activity Body { get; set; }
        
        [RequiredArgument]
        public Activity<Boolean> Condition { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="metadata"></param>
        protected override void CacheMetadata(NativeActivityMetadata metadata)
        {            
            metadata.AddChild(Body);
            metadata.AddChild(Condition);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            if (Condition != null){                
                context.ScheduleActivity<Boolean>(Condition, OnConditionComplete);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="completedInstance"></param>
        /// <param name="result"></param>
        private void OnConditionComplete(NativeActivityContext context, ActivityInstance completedInstance, Boolean result)
        {            
            if (!context.IsCancellationRequested){
                if (result && (Body != null)){                    
                    context.ScheduleActivity(Body, OnComplete, OnFaulted);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        /// <param name="completedInstance"></param>
        private void OnComplete(NativeActivityContext context, ActivityInstance completedInstance)
        {            
            if (!context.IsCancellationRequested){
                if (Condition != null){                    
                    context.ScheduleActivity<Boolean>(Condition, OnConditionComplete, OnFaulted);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="faultContext"></param>
        /// <param name="propagatedException"></param>
        /// <param name="propagatedFrom"></param>
        protected virtual void OnFaulted(NativeActivityFaultContext faultContext, Exception propagatedException, ActivityInstance propagatedFrom)
        {
            
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Cancel(NativeActivityContext context)
        {            
            if (context.IsCancellationRequested){                
                context.CancelChildren();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Abort(NativeActivityAbortContext context)
        {
            base.Abort(context);            
        }   
    }
}
